  {  Petrova Tatjana
     2013 / 01 / 15
     This is visualisation of sort.  }

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ExtCtrls, StdCtrls;
const
  n =15;
  w=15;
  h=7;
  x0=3;
  y0=3;

type
  TForm1 = class(TForm)
    Timer1: TTimer;
    procedure FormCreate(Sender: TObject);
    procedure Timer1Timer(Sender: TObject);
  private

  public
    procedure Draw;
  end;

var
  Form1: TForm1;
  arr : array  of integer;
  step,step2 : integer;


implementation





{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
var i : integer;
begin
  randomize;
  Setlength(arr,n);
  for i := 0 to (n-1) do
    arr[i]:=random(10);
end;

procedure TForm1.Draw;
var i : integer;
begin
  Form1.Refresh;
  for i := 0 to (n-1) do
  begin
    Form1.Canvas.Brush.Color := clWhite;
    if(i=step2) or (i=step2+1) then
       Form1.Canvas.Brush.Color := clRed;
    Form1.Canvas.Rectangle((x0+w*i),y0,(x0+w*(i+1)),y0+arr[i]*h);
  end;
  Sleep(10);
end;




procedure TForm1.Timer1Timer(Sender: TObject);
var k : integer;
begin
  Draw;
  if (arr[step2] > arr[step2+1]) then
  begin
    k := arr[step2];
    arr[step2] := arr[step2+1];
    arr[step2+1]:=k;
    Draw;
    sleep(100);
  end;
  step2 :=step2+1;
  if (step2=n-1-step)then
  begin
    step2:=0;
    step := step+1;
  end;
  if (step=n-1)then
    Timer1.Enabled := False;
end;


end.


